<?php
class Controller_Dailydeals extends Controller_Abstract
{
	public function actionIndex() {
		$dailyDealsModel = new DailyDeals();
		$this->_view['dailyDealsOffers'] = $dailyDealsModel->getAllDailyDealsOffers();
		$this->_view['sites'] = DataUtil::getOptionsForSites();
		if (isset($_SESSION['infos'])) {
			$this->_view['infos'] = $_SESSION['infos'];
			unset($_SESSION['infos']);
		}
	}

	public function actionAdd() {
		$this->_view['sites'] = DataUtil::getOptionsForSites();
		if ($this->_context->isPOST()) {
			$siteId = $this->_context->post('site_id');
			$PartnerName = $this->_context->post('partner_name', '');
			$titleText = $this->_context->post('title_text', '');
			$describeText = $this->_context->post('describe_text', '');
			$credit = $this->_context->post('credit', 0);
			$picture1Url = $this->_context->post('picture1_url', '');
			$picture2Url = $this->_context->post('picture2_url', '');
			$linkUrl = $this->_context->post('link_url', '');
			$startDate = $this->_context->post('start_date', '');
			$endDate = $this->_context->post('end_date', '');
			
			$dailyDealsModel = new DailyDeals();
			if ($dailyDealsModel->addDailyDealsOffer($siteId, $PartnerName, $titleText, $describeText, $credit, $picture1Url, $picture2Url, $linkUrl, $startDate, $endDate)) {
				$infos = array();
				$infos[] = Util::getMessage(18001);
				$_SESSION['infos'] = $infos;
				return $this->_redirect(url('dailydeals/index'));
			}
		}
	}
	
	public function actionUpdate() {
		$dailyDealsModel = new DailyDeals();
		if ($this->_context->isGET() && isset($this->_context->id)) {
			$dailyDealsOffer = $dailyDealsModel->getDailyDealsOfferById($this->_context->id);
			if ($dailyDealsOffer) {
				$this->_view['dailyDealsOffer'] = $dailyDealsOffer;
				$this->_view['sites'] = DataUtil::getOptionsForSites();
			}
		} else {
			$id = $this->_context->post('id');
			$siteId = $this->_context->post('site_id');
			$PartnerName = $this->_context->post('partner_name', '');
			$titleText = $this->_context->post('title_text', '');
			$describeText = $this->_context->post('describe_text', '');
			$credit = $this->_context->post('credit', 0);
			$picture1Url = $this->_context->post('picture1_url', '');
			$picture2Url = $this->_context->post('picture2_url', '');
			$linkUrl = $this->_context->post('link_url', '');
			$startDate = $this->_context->post('start_date', '');
			$endDate = $this->_context->post('end_date', '');

			if ($dailyDealsModel->updateDailyDealsOffer($siteId, $PartnerName, $titleText, $describeText, $credit, $picture1Url, $picture2Url, $linkUrl, $startDate, $endDate, $id)) {
				$infos = array();
				$infos[] = Util::getMessage(18002);
				$_SESSION['infos'] = $infos;
				return $this->_redirect(url('dailydeals/index'));
			}
		}
	}
	
	public function actionDelete() {
		$dailyDealsModel = new DailyDeals();
		$dailyDealsModel->deleteDailyDealsOffer($this->_context->id);
		$infos = array();
		$infos[] = Util::getMessage(18003);
		$_SESSION['infos'] = $infos;
		return $this->_redirect(url('dailydeals/index'));
	}
	
	public function actionPause() {
		$dailyDealsModel = new DailyDeals();
		$dailyDealsModel->pauseDailyDealsOffer($this->_context->id);
		return $this->_redirect(url('dailydeals/index'));
	}
	
	
}